export const formatTargetStructure = (creativePackage: any[]) => {
  // 构建返回的结果结构
  const result: any[] = [];
  
  // 处理每个创意包
  creativePackage.forEach((pkg) => {
    // 创建各个集合
    const items: any[] = [];
    const covers: any[] = [];
    const materials: any[] = [];
    const programmatic_creative_media_list: any[] = [];

    // 处理竖版视频
    if (pkg.verticalVideos && pkg.verticalVideos.length > 0) {
      pkg.verticalVideos.forEach((video: any) => {
        // 添加到items
        items.push({
          _id: video._id,
          Md5: video.Md5 || video.md5,
          CreateType: 1,
          CoverMd5: video.CoverMd5 || (video.Cover && (video.Cover.Md5 || video.Cover.md5)),
          IsMaterial: true,
          Name: video.Name,
          Thumb: video.Thumb,
          AspectRatio: 2 // 竖版视频比例
        });

        // 如果有封面，添加到covers
        if (video.Cover) {
          const coverData = {
            name: video.Cover.Name || "1",
            path: video.Cover.path || "",
            md5: video.Cover.Md5 || video.Cover.md5,
            width: video.Cover.Width || 720,
            height: video.Cover.Height || 1280,
            thumb: video.Cover.Thumb,
            previewUrl: video.Cover.previewUrl || video.Cover.Thumb,
            fileSize: video.Cover.FileSize || 0,
            type: "jpeg",
            isOss: true,
            csType: 3,
            Thumb: video.Cover.Thumb,
            Name: video.Cover.Name || "1",
            Md5: video.Cover.Md5 || video.Cover.md5,
            AspectRatio: 2,
            IsCover: true
          };
          covers.push(coverData);
          
          // 添加视频到materials
          materials.push({
            _id: video._id,
            Md5: video.Md5 || video.md5,
            CreateType: 1,
            CoverMd5: video.CoverMd5 || (video.Cover && (video.Cover.Md5 || video.Cover.md5)),
            IsMaterial: true,
            Name: video.Name,
            Thumb: video.Thumb,
            AspectRatio: 2
          });
          
          // 添加封面到materials
          materials.push({
            ...coverData,
            _id: coverData._id || video._id + 1000
          });
          
          // 添加到submitData
          programmatic_creative_media_list.push({
            image_mode: "VIDEO_VERTICAL",
            video_id: video.Md5 || video.md5,
            video_cover_id: video.Cover.Md5 || video.Cover.md5,
            image_ids: [],
            video_cover_width: video.Cover.Width || 720,
            video_cover_height: video.Cover.Height || 1280
          });
        }
      });
    }
    
    // 处理横版视频
    if (pkg.horizontalVideos && pkg.horizontalVideos.length > 0) {
      pkg.horizontalVideos.forEach((video: any) => {
        // 添加到items
        items.push({
          _id: video._id,
          Md5: video.Md5 || video.md5,
          CreateType: 1,
          CoverMd5: video.CoverMd5 || (video.Cover && (video.Cover.Md5 || video.Cover.md5)),
          IsMaterial: true,
          Name: video.Name,
          Thumb: video.Thumb,
          AspectRatio: 1 // 横版视频比例
        });

        // 如果有封面，添加到covers
        if (video.Cover) {
          const coverData = {
            name: video.Cover.Name || "1",
            path: video.Cover.path || "",
            md5: video.Cover.Md5 || video.Cover.md5,
            width: video.Cover.Width || 1280,
            height: video.Cover.Height || 720,
            thumb: video.Cover.Thumb,
            previewUrl: video.Cover.previewUrl || video.Cover.Thumb,
            fileSize: video.Cover.FileSize || 0,
            type: "jpeg",
            isOss: true,
            csType: 3,
            Thumb: video.Cover.Thumb,
            Name: video.Cover.Name || "1",
            Md5: video.Cover.Md5 || video.Cover.md5,
            AspectRatio: 1,
            IsCover: true
          };
          covers.push(coverData);
          
          // 添加视频到materials
          materials.push({
            _id: video._id,
            Md5: video.Md5 || video.md5,
            CreateType: 1,
            CoverMd5: video.CoverMd5 || (video.Cover && (video.Cover.Md5 || video.Cover.md5)),
            IsMaterial: true,
            Name: video.Name,
            Thumb: video.Thumb,
            AspectRatio: 1
          });
          
          // 添加封面到materials
          materials.push({
            ...coverData,
            _id: coverData._id || video._id + 1000
          });
          
          // 添加到submitData
          programmatic_creative_media_list.push({
            image_mode: "VIDEO_LARGE",
            video_id: video.Md5 || video.md5,
            video_cover_id: video.Cover.Md5 || video.Cover.md5,
            image_ids: [],
            video_cover_width: video.Cover.Width || 1280,
            video_cover_height: video.Cover.Height || 720
          });
        }
      });
    }
    
    // 处理大图
    if (pkg.largePic && pkg.largePic.length > 0) {
      pkg.largePic.forEach((img: any) => {
        // 添加到items
        items.push({
          _id: img._id,
          Md5: img.Md5 || img.md5,
          CreateType: 1,
          IsMaterial: true,
          Name: img.Name,
          Thumb: img.Thumb,
          AspectRatio: 2 // 大图比例
        });
        
        // 添加到materials
        materials.push({
          _id: img._id,
          Md5: img.Md5 || img.md5,
          CreateType: 1,
          IsMaterial: true,
          Name: img.Name,
          Thumb: img.Thumb,
          AspectRatio: 2
        });
        
        // 添加到submitData
        programmatic_creative_media_list.push({
          image_mode: "LARGE_VERTICAL",
          image_ids: [img.Md5 || img.md5],
          image_width: img.Width || 1080,
          image_height: img.Height || 1920
        });
      });
    }
    
    // 处理小图
    if (pkg.singlePic && pkg.singlePic.length > 0) {
      pkg.singlePic.forEach((img: any) => {
        // 添加到items
        items.push({
          _id: img._id,
          Md5: img.Md5 || img.md5,
          CreateType: 1,
          IsMaterial: true,
          Name: img.Name,
          Thumb: img.Thumb,
          AspectRatio: 1 // 小图比例
        });
        
        // 添加到materials
        materials.push({
          _id: img._id,
          Md5: img.Md5 || img.md5,
          CreateType: 1,
          IsMaterial: true,
          Name: img.Name,
          Thumb: img.Thumb,
          AspectRatio: 1
        });
        
        // 添加到submitData
        programmatic_creative_media_list.push({
          image_mode: "LARGE",
          image_ids: [img.Md5 || img.md5],
          image_width: img.Width || 1280,
          image_height: img.Height || 720
        });
      });
    }
    
    // 处理组图（三图）
    if (pkg.package3Pic && pkg.package3Pic.length > 0) {
      // 按照3张一组处理
      for (let i = 0; i < pkg.package3Pic.length; i += 3) {
        const group = pkg.package3Pic.slice(i, i + 3);
        
        // 只有当组中有3张图片时才处理
        if (group.length === 3) {
          const imageIds = group.map((img: any) => img.Md5 || img.md5);
          
          // 把组内所有图片添加到items和materials
          group.forEach((img: any) => {
            // 添加到items
            items.push({
              _id: img._id,
              Md5: img.Md5 || img.md5,
              CreateType: 1,
              IsMaterial: true,
              Name: img.Name,
              Thumb: img.Thumb,
              AspectRatio: 1 // 组图中每张图的比例
            });
            
            // 添加到materials
            materials.push({
              _id: img._id,
              Md5: img.Md5 || img.md5,
              CreateType: 1,
              IsMaterial: true,
              Name: img.Name,
              Thumb: img.Thumb,
              AspectRatio: 1
            });
          });
          
          // 一组三图对应一个submitData项
          programmatic_creative_media_list.push({
            image_mode: "GROUP_3",
            image_ids: imageIds,
            image_width: group[0].Width || 480,
            image_height: group[0].Height || 320
          });
        }
      }
    }
    
    // 将所有数据组装到结果中
    result.push({
      Items: items,
      Covers: covers,
      Materials: materials,
      SubmitData: {
        programmatic_creative_media_list
      }
    });
  });
  
  return result;
};

